package com.ruoyi.edu.listen;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.rabbitmq.client.Channel;
import com.ruoyi.common.core.constant.MqConstants;
import com.ruoyi.edu.api.domain.EduComment;
import com.ruoyi.edu.api.domain.EduCourse;
import com.ruoyi.edu.service.IEduCommentService;
import com.ruoyi.order.api.domain.TOrder;
import com.ruoyi.user.api.domain.UcenterMember;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 会员信息变更时，更新评论的会员信息
 *
 * @author Alex
 * @version 1.0
 * @date 2023/9/25 16:37
 */
@Service
public class UserMemberListen {

    @Resource
    private IEduCommentService iEduCommentService;

    /**
     * 订阅接收会员更新信息
     *
     * @param message 信息
     */
    @RabbitListener(queues = {MqConstants.MEMBER_QUEUE})
    public void receiveMemberMessage(Message message) {
        // 获取更新的会员信息
        UcenterMember member = JSON.parseObject(message.getBody(), UcenterMember.class);
        // 根据修改前的会员id，查询对应的订单并修改评论中的会员信息
        LambdaUpdateWrapper<EduComment> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EduComment::getMemberId, member.getId());
        // 如果没有对应的评论存在，直接结束即可
        if (iEduCommentService.count(wrapper) == 0) {
            return;
        }
        wrapper.set(EduComment::getNickname, member.getNickname());
        wrapper.set(EduComment::getAvatar, member.getAvatar());
        iEduCommentService.update(wrapper);
    }
}
